$(function () {
    $("body").on('click','#carousel-professional li',function () {//选择专业
        $(this).addClass("current").siblings().removeClass("current")
    }).on('click','.close',function(){
        $(this).parent('div').hide();
    }).on('click',"#carousel-subject li",function(){//选择科目
        $(this).toggleClass("current");
    }).on('click','#parentSubject',function () {//添加专业
        var parentList = $(".parentId");
        var parentIds ='';
        if(parentList!=null && parentList.length>0){
            parentIds = parentList.map(function () {
                return $(this).data('id');
            }).get().join(",");
        }
        $.ajax({
            url:baseloaction + '/web/uc/academic/getSubjectParent',
            type:'post',
            dataType:'json',
            data:{
                'parentIds':parentIds
            },
            success:function (result) {
                if(result.success==true){
                    var subjectList = result.entity;
                    //设置内容
                    var context = setParentListContext(subjectList);
                    var str ='增加专业';
                    icModale(2, '<strong>'+str+'</strong>', context, '确认', '520px', function(index, ele) {
                        var subjectIds =$('.current').data('id');
                        if(typeof  subjectIds =='undefined' || subjectIds==null||subjectIds==''){
                            icModale(0,"","请选择添加专业");
                            return;
                        }
                        saveUserSubjectData(subjectIds,index);
                    });
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，操作失败');
            }
        });
        //设置以专业内容
        function setParentListContext(parentSubjectList) {
            var context = '<section class="ml20 mr20">';
            context+='<div class="mt20 mb20 pb10">';
            context+='<div class="tabs-container phase-tabs">';
            context+='<ul class="nav nav-tabs text-center">';
            context+='<li class="active">';
            context+='<a>添加专业</a>';
            context+='<p class="c-ccc fs14 pb10">请选择你要关注的考试 (最多选择<span class="c-info"> 1 </span>项)</p>';
            context+='</li></ul>';
            context+='<div class="tab-content">';
            context+='<div class="tab-pane active">';
            context+='<div class="panel-body">';
            context+='<div class="mr20 ml20">';
            context+='<section>';
            context+='<div class="carousel slide carousel-class-member__wrap carousel-subject_wrap" id="carousel-professional">';
            context+='<div class="carousel-inner">';
            context+='<div class="item" style="display:block"><ol>';
            $.each(parentSubjectList,function (index,em) {
                context+='<li data-id="'+em.id+'" data-parent="0">';
                context+='<a href="javascript:void (0);" class="regTxt">'+em.subjectName+'</a>';
                context+='</li>';
            });
            context+='</ol></div></div></div></section></div></div></div></div></div></div></section>';
            return context;

        }
    }).on('click','.addChildSubject',function () {//添加科目
        var _this = $(this);
        var parentId = _this.data('parent');
        var childList = $(".child-subject");
        var childIds ='';
        if(childList!=null && childList.length>0){
            childIds = childList.map(function () {
                return $(this).data('child');
            }).get().join(",");
        }
        $.ajax({
            url:baseloaction +'/web/uc/academic/ajax/querySubjectByParentIds',
            type:'post',
            dataType:'json',
            data:{
                'parentId':parentId,
                'childIds':childIds
            },
            success:function (result) {
                if(result.success==true){
                    var childList = result.entity;
                    //设置内容
                    var context = setChildListContext(childList);
                    var str ='增加科目';
                    icModale(2, '<strong>'+str+'</strong>', context, '确认', '520px', function(index, ele) {
                        var list =$('.current');
                        if(list==null || list.length <=0 ){
                            icModale(0,'','请选择一个或多个科目');
                            return;
                        }
                        var subjectIds = list.map(function () {
                            return $(this).data('id');
                        }).get().join(',');
                        subjectIds = parentId + ","+subjectIds;
                        saveUserSubjectData(subjectIds,index);
                    });
                }else{
                    icModale(0,'',result.message);
                }
            },
            error:function (error) {
                icModale(0,'','系统错误，获取科目数据失败');
            }
        });
        //获取科目配置内容
        function setChildListContext(subjectList) {
            var context = '<section class="ml20 mr20">';
            context+='<div class="mt20  mb20 pb10">';
            context+='<div class="tabs-container phase-tabs ">';
            context+='<ul class="nav nav-tabs text-center">';
            context+='<li class="active pb10">';
            context+='<a>完善科目</a>';
            context+='<p class="c-ccc fs14  pb10">请选择你要最感兴趣或者最想学的科目，可以<span class="c-info">多选</span>哦</p>';
            context+='</li></ul>';
            context+='<div class="tab-content">';
            context+='<div class="tab-pane active">';
            context+='<div class="panel-body">';
            context+='<div class="">';
            context+='<section>';
            context+='<div class="carousel slide carousel-class-member__wrap carousel-subject_wrap" id="carousel-subject">';
            context+='<div class="carousel-inner">';
            context+='<div class="item" style="display:block">';
            context+='<ol>';
            $.each(subjectList,function (index,subject) {
                context+='<li data-id="'+subject.id+'">';
                context+='<a href="javascript:void (0);" class="regTxt">'+subject.subjectName+'</a><i class="fa fa-check-square c-info fs18 checked"></i>';
                context+='</li>';
            });
            context+='</ol></div></div></div></section></div></div></div></div></div></div></section>';
            return context;
        }
    });

    /**
     * 保存用户的专业/科目数据
     * @param subjectIds
     */
    function saveUserSubjectData(subjectIds,index) {
        $.ajax({
            url: baseloaction + "/web/uc/academic/save/userAddSubject",
            data: {"subjectIds": subjectIds},
            type: "post",
            dataType: "json",
            success:function (result) {
                if(result.success==true){
                    layer.close(index);
                    //重新加载页面
                    getMyAcademic();
                }else{
                    icModale(0, "", result.message);
                }
            }
        });
    }
});

/**
 * 加载学业信息
 */
function getMyAcademic() {
    //获取基础专业
    getPageAjaxData({
        url: baseloaction + "/web/uc/academic/ajax/myAcademic",
        contextId:'ajaxContext',
        callback: function () {
        }
    });
}